<?php

class Planesp_Model extends CI_Model {

    function grabarplan($parameters, $base) {
        $id = 0;

        $insert = array(
            "nro_cuotas_plan" => $parameters['nro_cuotas_plan'],
            "valor_financiar" => $parameters['valor_financiar'],
            "couta1_plan" => $parameters['couta1_plan'],
            "couta2_plan" => $parameters['couta2_plan'],
            "couta3_plan" => $parameters['couta3_plan'],
            "couta4_plan" => $parameters['couta4_plan'],
            "couta5_plan" => $parameters['couta5_plan'],
            "cuota6_plan" => $parameters['cuota6_plan'],
            "cuota7_plan" => $parameters['cuota7_plan'],
            "matriculas_id_matr" => $parameters['matriculas_id_matr']);
        $this->db->trans_start();

        $this->db->insert($base, $insert);
        $id = $this->db->insert_id();
        $this->db->trans_complete();

        return $id;
    }

    function actualizarPlan($parameters, $id_matr, $base) {
        $update = array(
            "nro_cuotas_plan" => $parameters['nro_cuotas_plan'],
            "valor_financiar" => $parameters['valor_financiar'],
            "couta1_plan" => $parameters['couta1_plan'],
            "couta2_plan" => $parameters['couta2_plan'],
            "couta3_plan" => $parameters['couta3_plan'],
            "couta4_plan" => $parameters['couta4_plan'],
            "couta5_plan" => $parameters['couta5_plan'],
            "cuota6_plan" => $parameters['cuota6_plan'],
            "cuota7_plan" => $parameters['cuota7_plan']);

        $this->db->trans_start();
        $this->db->where("matriculas_id_matr", $id_matr);
        $this->db->update($base, $update);
        $this->db->trans_complete();
    }
    
       
    function actualizarCuotaCancelada($nro_coutas_canc, $ide_plan, $base) {
        $cadsql="UPDATE " . $base ." SET nro_coutas_canc = nro_coutas_canc +". $nro_coutas_canc
                ." WHERE ide_plan =". $ide_plan;

        $this->db->trans_start();
        $this->db->query($cadsql);
        $this->db->trans_complete();
    }

    function eliminarPlan($id_matr, $base) {
        $this->db->trans_start();
        $update = array("eliminado" => 1);
        $this->db->where("matriculas_id_matr", $id_matr);
        $this->db->update($base, $update);
        $this->db->trans_complete();
    }

    function eliminaFisicaPlan($id_matr, $base) {
        $this->db->trans_start();
        $this->db->where("matriculas_id_matr", $id_matr);
        $this->db->delete($base);
        $this->db->trans_complete();
    }

    function VerificarPlan($base, $id_matr) {
        $encontrado = TRUE;
        $this->db->select('ide_plan');
        $this->db->from($base);
        $this->db->where('matriculas_id_matr', $id_matr);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            $encontrado = TRUE;
        } else {
            $encontrado = FALSE;
        }
        $query->free_result();
        return $encontrado;
    }

    /*Modificado por jose y patricio 06-abril*/
    function getPlanPagos($base, $id_matr) { // Se envia de parametro la tabla plan a la cual se conecta
        $aux='';
        if($base=='planes_pago_semestre'){
            $aux = ','.$base.'.pago_inicial,'.$base.'.nro_couta_inicial,';
        }else{
            $aux = ','.$base.'.pago_inicial'.','.$base.'.incr_decre_plan'.','.$base.'.tipo_incre_plan';
            
        }
        
        $this->db->select($base . '.ide_plan,' . $base . '.nro_cuotas_plan,' . $base . '.nro_coutas_canc,' . $base . '.valor_financiar,' .
                $base . '.couta1_plan,' . $base . '.couta2_plan,' . $base . '.couta3_plan,' .
                $base . '.couta4_plan,' . $base . '.couta5_plan,' . $base . '.cuota6_plan,' .
                $base . '.cuota7_plan'.$aux);
        $this->db->distinct();
        $this->db->from('matriculas');
        $this->db->join('cursa', 'matriculas.id_matr = cursa.matriculas_id_matr', 'inner');
        $this->db->join($base, 'cursa.matriculas_id_matr = ' . $base . '.matriculas_id_matr', 'inner');
        $this->db->where('matriculas.id_matr ', $id_matr);
        $query = $this->db->get();
      //  echo $this->db->last_query();
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $data[] = $row; //$row;
            }
            $query->free_result();
            return $data;
        } else {
            return null;
        }
    }
        function getPlan($id_matricula,$tabla){
        $this->db->from($tabla);
        $this->db->where('matriculas_id_matr ', $id_matricula);
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $data[] = $row; //$row;
            }
            $query->free_result();
            return $data;
        } else {
            return array();
        }
    }
    function actualizarPlanC($parameters, $id_matr, $base) {       

        $this->db->trans_start();
        $this->db->where("matriculas_id_matr", $id_matr);
        $this->db->update($base, $parameters);
        $this->db->trans_complete();
    }
//fin jose
    function grabarpagoinicial($valor, $id_matr, $base) { // grabar el pago inicial del arrastre o titulacion  al matricularse 
        $update = array(
            "pago_inicial" => $valor);

        $this->db->trans_start();
        $this->db->where("matriculas_id_matr", $id_matr);
        $this->db->update($base, $update);
        $this->db->trans_complete();
    }
    function getpagoinicial($id_matr) { // recuperar el pago inicial del arrastre o titulacion  al matricularse 
        $this->db->select('pago_inicial');
        $this->db->from('planes_pago_arrastre');
        $this->db->where('matriculas_id_matr', $id_matr);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $data[] = $row; //$row;
            }
            $pago_inicial = $data[0]['pago_inicial'];
        }else{
            $pago_inicial=0;
        }
        $query->free_result();
        return $pago_inicial;
    }
    

    function grabarincr_decre($valor, $id_matr, $base) { // grabar el incremento o decremento dado por conflictos en creditos
        $update = array(
            "incr_decre_plan" => $valor);

        $this->db->trans_start();
        $this->db->where("matriculas_id_matr", $id_matr);
        $this->db->update($base, $update);
        $this->db->trans_complete();
    }
    function getincr_decre($valor, $id_matr, $base) { // recuperar el incremento o decremento dado por conflictos en creditos
        $this->db->select('incr_decre_plan');
        $this->db->from('planes_pago_arrastre');
        $this->db->where('matriculas_id_matr', $id_matr);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $data[] = $row; //$row;
            }
            $incr_decre_plan = $data[0]['incr_decre_plan'];
        }else{
            $incr_decre_plan=0;
        }
        $query->free_result();
        return $incr_decre_plan;
    }
    

    function grabartipo_incre($valor, $id_matr, $base) { // grabar el tipo incremento o decremento dado por conflictos en creditos
        $update = array(
            "tipo_incre_plan" => $valor);

        $this->db->trans_start();
        $this->db->where("matriculas_id_matr", $id_matr);
        $this->db->update($base, $update);
        $this->db->trans_complete();
    }
    function gettipo_incre($id_matr) { // recuoerar el tipo incremento o decremento dado por conflictos en creditos
        $this->db->select('tipo_incre_plan');
        $this->db->from('planes_pago_arrastre');
        $this->db->where('matriculas_id_matr', $id_matr);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $data[] = $row; //$row;
            }
            $tipo_incre_plan = $data[0]['tipo_incre_plan'];
        }else{
            $tipo_incre_plan=0;
        }
        $query->free_result();
        return $tipo_incre_plan;
    }    
    
    function actualizarPlanRefinanciamento($parameters, $id_plan, $tabla) {
        
        $this->db->where("ide_plan", $id_plan);
        $this->db->update($tabla, $parameters);
        //$this->db->trans_complete();
    }
    //patricio 02 de junio
        function getide_coutas($ide_plan,$tabla,$tabla1) { // recuoerar el tipo incremento o decremento dado por conflictos en creditos
        $ide_canc=0;
        $this->db->select('ide_canc');
        $this->db->from($tabla);
        $this->db->where($tabla1.'_ide_plan', $ide_plan);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $data[] = $row; //$row;
            }
            $ide_canc = $data[0]['ide_canc'];
        }else{
            $ide_canc=0;
        }
        $query->free_result();
        return $ide_canc;
    }    

 }
